#!/usr/bin/env python

import os, shutil

#this defines the location of each folder after accessing the root directory of the mounted image
location	= {
	'messages': '/Volumes/Data/mobile/Library/SMS/sms.db',
	'addressBookData': '/Volumes/Data/mobile/Library/AddressBook/AddressBook.sqlitedb',
	'notes':'/Volumes/Data/mobile/Library/Notes/notes.sqlite',
	'maps':'/Volumes/Data/mobile/Library/Caches/Maps/MapTiles/MapTiles.sqlitedb',
	'callHistory':'/Volumes/Data/wireless/Library/CallHistory/call_history.db',
	'calendar':	'/Volumes/Data/mobile/Library/Calendar/Calendar.sqlitedb',
}


def createFolder(f):
    try:
    	path = f
    	os.mkdir(os.path.expanduser(path))
    	print('Folder created at {}'.format(path))	

    except OSError:
    	print('Folder already exists')	


def headerPrint(val):
	print('--------------------------------------------------------------------------------------------------------')	
	print(val);
	print('--------------------------------------------------------------------------------------------------------')

#The functions are defined in the order they are asked for in the menu

#messages
def messages():
	createFolder('~/Desktop/FYPFiles/messages')
	createFolder('~/Desktop/FYPFiles/messages/Sample Data')
	createFolder('~/Desktop/FYPFiles/messages/Results')
	
	#now navigate inside the mounted image, copy the necessary image
	#here the sms.db
	
	#src = "/Volumes/Data/mobile/Library/SMS/sms.db"
	src = location.get('messages')
	dest = "/Users/vikas/Desktop/FYPFiles/messages/Sample Data"

	shutil.copy2(src, dest)
	print("done")

	#time to call our sms.py script and sms_deleted.py script
	#how to do that? :|
	#use os.system

	os.chdir('/Users/vikas/Desktop/FYPFiles/messages/Sample Data')
	
	#run the sms.py
	headerPrint('Displaying existing messages')
	os.system('sms.py')

	#run the sms_delete.py
	headerPrint('Displaying deleted messages')
	os.system('sms_delete.py')	

#AddressBook Data
def addressBookData():
	createFolder('~/Desktop/FYPFiles/AddressBookData')
	createFolder('~/Desktop/FYPFiles/AddressBookData/Sample Data')
	createFolder('~/Desktop/FYPFiles/AddressBookData/Results')
	
	#now navigate inside the mounted image, copy the necessary image
	#here the sms.db
	
	#src = "/Volumes/Data/mobile/Library/SMS/sms.db"
	src = location.get('addressBookData')
	dest = "/Users/vikas/Desktop/FYPFiles/AddressBookData/Sample Data"

	shutil.copy2(src, dest)
	print("done")

	#time to call our sms.py script and sms_deleted.py script
	#how to do that? :|
	#use os.system

	os.chdir('/Users/vikas/Desktop/FYPFiles/AddressBookData/Sample Data')
	
	#run the AddressBook.py
	headerPrint('Displaying existing address book data')
	os.system('AddressBook.py')

	# #run the sms_delete.py
	# headerPrint('Displaying deleted messages')
	# os.system('python sms_delete.py sms.db')	

#now the same drill for notes db
def notes():
	createFolder('~/Desktop/FYPFiles/Notes')
	createFolder('~/Desktop/FYPFiles/Notes/Sample Data')
	createFolder('~/Desktop/FYPFiles/Notes/Results')
	
	#now navigate inside the mounted image, copy the necessary image
	#here the sms.db
	
	#src = "/Volumes/Data/mobile/Library/SMS/sms.db"
	src = location.get('notes')
	dest = "/Users/vikas/Desktop/FYPFiles/Notes/Sample Data"

	shutil.copy2(src, dest)
	print("done")


	os.chdir('/Users/vikas/Desktop/FYPFiles/Notes/Sample Data')
	
	#run the AddressBook.py
	headerPrint('Displaying existing address book data')
	os.system('notes.py')

	# #run the sms_delete.py
	# headerPrint('Displaying deleted messages')
	# os.system('python sms_delete.py sms.db')	

#now the same drill for notes db
def notes():
	createFolder('~/Desktop/FYPFiles/Notes')
	createFolder('~/Desktop/FYPFiles/Notes/Sample Data')
	createFolder('~/Desktop/FYPFiles/Notes/Results')
	
	#now navigate inside the mounted image, copy the necessary image
	#here the sms.db
	
	#src = "/Volumes/Data/mobile/Library/SMS/sms.db"
	src = location.get('notes')
	dest = "/Users/vikas/Desktop/FYPFiles/Notes/Sample Data"

	shutil.copy2(src, dest)
	print("done")


	os.chdir('/Users/vikas/Desktop/FYPFiles/Notes/Sample Data')
	
	#run the AddressBook.py
	headerPrint('Displaying existing Notes')
	os.system('notes.py')

	# #run the sms_delete.py
	# headerPrint('Displaying deleted messages')
	# os.system('python sms_delete.py sms.db')	


#fr getting the maps we also need the perl script
#I m copying this from my working copy and placing it in the same folder as the sample data 
def maps():
	createFolder('~/Desktop/FYPFiles/Maps')
	createFolder('~/Desktop/FYPFiles/Maps/Sample Data')
	createFolder('~/Desktop/FYPFiles/Maps/Results')
	
	#now navigate inside the mounted image, copy the necessary image
	#here the sms.db
	
	#src = "/Volumes/Data/mobile/Library/SMS/sms.db"
	src = location.get('maps')
	dest = "/Users/vikas/Desktop/FYPFiles/Maps/Sample Data"

	shutil.copy2(src, dest)
	print("done")

	src = '/Users/vikas/Documents/FYP/Maps/Scripts/parse_maptiles.pl'
	dest= "/Users/vikas/Desktop/FYPFiles/Maps/Sample Data" 


	shutil.copy2(src, dest)
	print("perl script copied")

	os.chdir('/Users/vikas/Desktop/FYPFiles/Maps/Sample Data')
	
	#run the Maps.py
	headerPrint('Displaying existing map tiles')
	os.system('maps.py')

	headerPrint('Success!')
	headerPrint('Please refer to the working directory for the maptiles-output folder')

	# #run the sms_delete.py
	# headerPrint('Displaying deleted messages')
	# os.system('python sms_delete.py sms.db')	
	

#call history
def callHistory():
	createFolder('~/Desktop/FYPFiles/CallHistory')
	createFolder('~/Desktop/FYPFiles/CallHistory/Sample Data')
	createFolder('~/Desktop/FYPFiles/CallHistory/Results')
	
	
	src = location.get('callHistory')
	dest = "/Users/vikas/Desktop/FYPFiles/CallHistory/Sample Data"

	shutil.copy2(src, dest)
	print("done")


	os.chdir('/Users/vikas/Desktop/FYPFiles/CallHistory/Sample Data')
	
	#run the AddressBook.py
	headerPrint('Displaying existing call history records')
	os.system('call_history.py')

	# #run the sms_delete.py
	# headerPrint('Displaying deleted messages')
	# os.system('python sms_delete.py sms.db')	

def calendar():
	createFolder('~/Desktop/FYPFiles/Calendar')
	createFolder('~/Desktop/FYPFiles/Calendar/Sample Data')
	createFolder('~/Desktop/FYPFiles/Calendar/Results')
	
	
	src = location.get('calendar')
	dest = "/Users/vikas/Desktop/FYPFiles/Calendar/Sample Data"

	shutil.copy2(src, dest)
	print("done")


	os.chdir('/Users/vikas/Desktop/FYPFiles/Calendar/Sample Data')
	
	#run the AddressBook.py
	headerPrint('Displaying existing calendar events records')
	os.system('calendar.py')

	# #run the sms_delete.py
	# headerPrint('Displaying deleted messages')
	# os.system('python sms_delete.py sms.db')	

def main():
	
	#create the parent folder if it does not exist
	createFolder("~/Desktop/FYPFiles")

	#now check if you have the dmg in the folder, then mount it in read only mode
	os.chdir("/Users/vikas/iphone-dataprotection/6472cce3c397409085aaae0ca99bc216eb78cb20")
	os.system('hdid -readonly iphonedump2.dmg')

	#ask the user what he wants to do
	selection = raw_input('Select the data that you want to retrieve \n1) messages \n2) addressbook data \n3) Notes \n4)Maps \n5)Call History \n6)Calendar \n')

	if selection =='1':
		print ('messages')
		messages()

	elif selection =='2':
		print('addressbook')	
		addressBookData()

	elif selection =='3':
		print('Notes')	
		notes()
		
	elif selection =='4':
		print('Maps')	
		maps()

	elif selection =='5':
		print('Call History')	
		callHistory()

	elif selection =='6':
		print('Calendar')
		calendar()		

if __name__ == '__main__':
	main()